<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="news" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- default-expand-all这个代码可以展开子节点 -->
        <el-tree
          default-expand-all
          :data="list"
        >
          <template #default="{data}">

            <el-row type="flex" justify="space-between" align="middle" style="height: 40px;width:100%">
              <el-col :span="20">
                <svg-icon icon-class="news" /><span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑子部门</el-dropdown-item>
                        <!-- 加入if如果有子级就隐藏删除按钮 -->
                        <el-dropdown-item
                          v-if="data.children.length===0"
                          @click.native="hDel(data.id)"
                        >删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <!-- 这样子可以设置动态标题 -->
      <el-dialog
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :title="isEdit ? '编辑部门':'新增部门'"
        :visible.sync="dialogVisible"
        width="30%"
        @closed="hclosed"
      >
        <!-- 子组件 -->
        <!-- 这个v-if可以解决表单重置问题跟dialogVisible绑定但是创建销毁有性能 -->
        <!-- v-if="dialogVisible" -->
        <addorEdit
          v-if="dialogVisible"
          :id="curId"
          :is-edit="isEdit"
          :orgin-list="orginList"
          @close="dialogVisible=false"
          @success="hSuccess"
        />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { transDataToTreeData } from '@/utils/index'
import { getDepartments, delDepartments } from '@/api/departments'
import addorEdit from './deptDialog.vue'
export default {
  components: {
    addorEdit
  },
  data() {
    return {
      list: [],
      dialogVisible: false,
      curId: '',
      // false是新增 ture是编辑
      isEdit: false,
      orginList: []
    }
  },
  created() {
    this.loadDepartments()
  },
  methods: {
    hclosed() {
      this.$refs.depRef.resetFrom()
    },
    // 删除部门
    async hDel(id) {
      const res = await delDepartments(id)
      if (res.code === 10000) {
        this.$message.success(res.message)
        this.loadDepartments()
      }
    },
    // 重新渲染页面并且关闭窗口
    hSuccess() {
      this.loadDepartments()
      this.dialogVisible = false
    },
    hEdit(id) {
      this.curId = id
      this.isEdit = true
      this.dialogVisible = true
    },
    // 获取到添加部门需要用到的id 然后父传子传过去
    hAdd(id) {
      // console.log(id)
      this.curId = id
      this.isEdit = false
      this.dialogVisible = true
    },
    handleNodeClick(data) {
      console.log(data)
    },
    async  loadDepartments() {
      try {
        const res = await getDepartments()
        // console.log(res, 'hahah')
        if (res.code !== 10000) {
          return this.$message.error(res.message)
        }
        // 用map重新返回出来一个新的数组并且传给子组件 到子组件里面使用
        this.orginList = res.data.depts.map(item => {
          const { id, code, name, pid } = item
          return { id, code, name, pid }
        })
        this.$message.success(res.message)
        res.data.depts.shift()
        this.list = transDataToTreeData(res.data.depts)
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>
